﻿/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using Daidalos.Exception;
using System.Collections.Generic;
using Microsoft.DirectX.Direct3D;

namespace Daidalos.Components
{

    public class Window : Component
    {

        #region Enumerations
        public enum WindowStyle
        {
            ToolWindow,
            Default
        }
        #endregion

        #region Attributes
        /// <summary>
        ///     The type of the window.
        /// </summary>
        protected WindowStyle type;

        protected Font.BitmapString font;
        #endregion

        #region Constructors
        public Window(string label, float left, float top, float width, float height, WindowStyle type)
            : base(label, left, top, width, height)
        {
            // Store the window type.
            this.type = type;
            font = new Daidalos.Font.BitmapString(label, "arial", 1.0f, System.Drawing.Color.White);
        }
        #endregion

        #region Methods
        /// <summary>
        ///     This method overrides the display method of the
        ///     base class.
        /// </summary>
        /// <remarks>
        ///     Rendering of a window depends on the underlying
        ///     window types.
        /// </remarks>
        public override void Display()
        {
            // TODO: Implement tool window design!
            if (type == WindowStyle.ToolWindow)
            {
                throw new MissingFeaturesException();
            }

            // Render as default window.
            if (type == WindowStyle.Default)
            {
                // This is debug rendering (and sparta, among other things!). Mainly this is <blink>SLOW!</blink>
                CustomVertex.TransformedColored[] v = new CustomVertex.TransformedColored[6];

                // Fetch coordinates.
                float left = this.left;
                float top = this.top;
                float bottom = top + height;
                float right = left + width;

                // Titlebar
                v[0].X = left; v[0].Y = top;
                v[2].X = left; v[2].Y = bottom;
                v[1].X = right; v[1].Y = bottom;

                v[3] = v[0];
                v[4].X = right; v[4].Y = top;
                v[5] = v[1];

                // I'm so lazy...
                for (int i = 0; i < v.Length; ++i) { v[i].Z = 1; v[i].Rhw = 1; v[i].Color = System.Drawing.Color.PowderBlue.ToArgb(); }

                Graphics.RenderDevice.Instance.Device.VertexFormat = CustomVertex.TransformedColored.Format;
                Graphics.RenderDevice.Instance.Device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, v);
            }

            // Panels are invisible, therefore they don't paint themselves, but their children.
            foreach (Component child in children)
            {
                // Invoke the display method of the child.
                child.Display();
            }
        }
        #endregion

    }

}
